import { useState, useMemo } from 'react';
import moment from 'moment';

interface useDateRangeConfig {
  begin: number;
  end: number;
  defaultStartTime: number;
  defaultEndTime: number;
}
// 用于控制时间选择起
const Hours = Array.from(Array(24), (v, k) => k);
const Minutes = Array.from(Array(60), (v, k) => k);
const Seconds = Array.from(Array(60), (v, k) => k);

/**
 * 业务方法 项目中一般用秒级
 * @param time 时间戳 秒
 */
export const handleSecondTime = (time: number) => {
  return moment(time * 1000);
};

interface DisableState {
  begin: number; // 限制起始时间 0代表不限制
  end: number; // 限制终止时间  0代表不限制
  currentDate: any; // 限制日期需要
}

// 限制日期
export const disableDate = (params: DisableState): boolean => {
  const { currentDate, begin, end } = params;
  return (
    currentDate < handleSecondTime(begin).endOf('day').subtract(1, 'days') ||
    currentDate > handleSecondTime(end)
  );
};

// 限制时间--hour
export const disableHours = (params: DisableState): number[] => {
  const { begin, end, currentDate } = params;

  if (!currentDate) return [];
  let disHours: number[] = [];
  // 日期小于后面的限制 不限制小时段
  const currentDay = handleSecondTime(currentDate).date();
  const beginDay = handleSecondTime(begin).date();
  const endDay = handleSecondTime(end).date();

  const beginH = handleSecondTime(begin).hour();
  const endH = handleSecondTime(end).hour();

  // 和限制最后天
  if (currentDay === endDay) {
    const endHours = Hours.slice(endH + 1, Hours.length);
    disHours = [...disHours, ...endHours];
  }

  // 和限制前面天
  if (currentDay === beginDay) {
    const beginHours = Hours.slice(0, beginH);
    disHours = [...disHours, ...beginHours];
  }

  // 天数处于中间没有限制必要

  return disHours;
};

// 限制时间--mins
export const disableMins = (params: DisableState): number[] => {
  const { begin, end, currentDate } = params;
  let disMins: number[] = [];
  if (!currentDate) return [];

  const currentDay = handleSecondTime(currentDate).date();
  const beginDay = handleSecondTime(begin).date();
  const endDay = handleSecondTime(end).date();

  const currentHour = handleSecondTime(currentDate).hour();
  const beginHour = handleSecondTime(begin).hour();
  const endHour = handleSecondTime(end).hour();

  // 同一天 同一个小时才有限制的必要
  if (currentDay === endDay && currentHour === endHour) {
    const m = handleSecondTime(end).minute();
    const mins = Minutes.slice(m + 1, Minutes.length);
    disMins = [...disMins, ...mins];
  }

  if (currentDay === beginDay && currentHour === beginHour) {
    const m = handleSecondTime(begin).minute();
    const mins = Minutes.slice(0, m);
    disMins = [...disMins, ...mins];
  }

  // 中间天数 、 时段没有限制必要
  return disMins;
};

// 限制时间--mins
export const disableSeconds = (params: DisableState): number[] => {
  const { begin, end, currentDate } = params;
  const currentDay = handleSecondTime(currentDate).date();
  const beginDay = handleSecondTime(begin).date();
  const endDay = handleSecondTime(end).date();

  const currentHour = handleSecondTime(currentDate).hour();
  const beginHour = handleSecondTime(begin).hour();
  const endHour = handleSecondTime(end).hour();

  const currentMin = handleSecondTime(currentDate).minute();
  const beginMin = handleSecondTime(begin).minute();
  const endMin = handleSecondTime(end).minute();

  let disMins: number[] = [];

  if (!currentHour || !currentMin) return [];

  if (currentDay === endDay && currentHour === endHour && currentMin === endMin) {
    const s = handleSecondTime(end).second();
    const seconds = Seconds.slice(s + 1, Seconds.length);
    disMins = [...disMins, ...seconds];
  }
  if (currentDay === beginDay && currentHour === beginHour && currentMin === beginMin) {
    const s = handleSecondTime(begin).second();
    const seconds = Seconds.slice(0, s);
    disMins = [...disMins, ...seconds];
  }

  return disMins;
};

const useDateRange = (config: useDateRangeConfig) => {
  const { begin, end, defaultStartTime, defaultEndTime } = config;

  const [startTime, setStartTime] = useState<number>(defaultStartTime);
  const [endTime, setEndTime] = useState<number>(defaultEndTime);

  // 限制日期（起始）
  const disableBeginDate = (currentDate: any): boolean => {
    return disableDate({
      begin,
      end,
      currentDate,
    });
  };

  // 限制时间--hour （起始）
  const disableBeginHours = (): number[] => {
    return disableHours({
      begin,
      end,
      currentDate: startTime,
    });
  };
  // 限制时间--mins （起始）
  const disableBeginMins = (): number[] => {
    return disableMins({
      begin,
      end,
      currentDate: startTime,
    });
  };
  // 限制时间--seconds （起始）
  const disableBeginSeconds = (): number[] => {
    return disableSeconds({
      begin,
      end,
      currentDate: startTime,
    });
  };

  // 限制日期（终止）
  const disableEndDate = (currentDate: any): boolean => {
    return disableDate({
      begin,
      end,
      currentDate,
    });
  };
  // 限制时间--hour （终止）
  const disableEndHours = (): number[] => {
    return disableHours({
      begin,
      end,
      currentDate: endTime,
    });
  };
  // 限制时间--mins （终止）
  const disableEndMins = (): number[] => {
    return disableMins({
      begin,
      end,
      currentDate: endTime,
    });
  };
  // 限制时间--seconds （起始）
  const disableEndSeconds = (): number[] => {
    return disableSeconds({
      begin,
      end,
      currentDate: endTime,
    });
  };

  const transTime = (time: number, direct: 'begin' | 'end') => {
    let transStartTime: number = 0;
    let transEndTime: number = 0;
    if (direct === 'begin') {
      transStartTime = time < endTime ? time : endTime;
      transEndTime = time < endTime ? endTime : time;
    }
    if (direct === 'end') {
      transStartTime = time < startTime ? time : startTime;
      transEndTime = time < startTime ? startTime : time;
    }

    transStartTime = transStartTime < begin ? begin : transStartTime;
    transEndTime = transEndTime > end ? end : transEndTime;

    setStartTime(transStartTime);
    setEndTime(transEndTime);
  };

  const setRangeStartTime = (time: number) => {
    // 转换
    transTime(time, 'begin');
  };
  const setRangeEndTime = (time: number) => {
    // 转换
    transTime(time, 'end');
  };

  return useMemo(() => {
    return {
      startTime,
      endTime,
      setStartTime,
      setEndTime,
      setRangeStartTime,
      setRangeEndTime,
      disableBeginDate,
      disableEndDate,
      disableBeginHours,
      disableBeginMins,
      disableBeginSeconds,
      disableEndHours,
      disableEndMins,
      disableEndSeconds,
    };
  }, [startTime, endTime]);
};

export default useDateRange;
